package com.example.springbootdruid.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.zaxxer.hikari.util.DriverDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {
    // 注冊进入bean并且关联配置文件
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //配置sql监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean =
                new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后台登录需要设置账号密码
        HashMap<String, String> initParameters = new HashMap<>();
        //添加配置
        //loginUsername跟loginPassword是固定的写死的
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");
        //所有人都可以访问
        initParameters.put("allow","");
        bean.setInitParameters(initParameters);//初始化参数
        return bean;

    }
}
